# Build recipe for musl.
#
# Copyright (c) 2015-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=musl
version=1.2.3
release=1

# Define a category for the output of the package name
pkgcategory=libs

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=https://www.musl-libc.org/releases/$tarname
#fetch="
#  https://dragora.mirror.garr.it/current/sources/$tarname
#  rsync://rsync.dragora.org/current/sources/$tarname
#"

description="
A powerful standard C/POSIX library (version $version).

Musl is a new standard library to power a new generation of Linux-based
devices.  Musl is lightweight, fast, simple, free, and strives to be
correct in the sense of standards-conformance and safety.
"

homepage=https://www.musl-libc.org
license="MIT Expat variant"

# Source documentation
docs="COPYRIGHT README VERSION WHATSNEW"
docsdir="${docdir}/${program}"

# Limit package name to the program name
full_pkgname="${program}@${pkgcategory}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    patch -Np0 -i "${worktree}/patches/musl/musl-utmp_path.diff"
    #patch -Np1 -i "${worktree}/patches/musl/branch-updates.diff"

    # Do not use the compiler runtime library from the temporary system,
    # the toolchain must be adjusted until the native GCC take place
    if cc -print-libgcc-file-name | grep -q '^/tools'
    then
        patch -Np1 -i "${worktree}/patches/musl/musl-nolibcc_stage1.diff"
    fi

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --syslibdir=/lib \
     --enable-shared \
     --enable-static \
     --enable-optimize

    make -j${jobs}
    make -j${jobs} install DESTDIR="$destdir"

    # Provide minimal libssp_nonshared.a

    cc -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
       -o __stack_chk_fail_local.o
    ar rc libssp_nonshared.a __stack_chk_fail_local.o
    ranlib libssp_nonshared.a
    cp -p libssp_nonshared.a "${destdir}/usr/lib${libSuffix}"
    strip -g "${destdir}/usr/lib${libSuffix}/libssp_nonshared.a"

    # To print shared library dependencies
    mkdir -p "${destdir}/usr/bin"
    ln -sf /usr/lib${libSuffix}/libc.so "${destdir}/usr/bin/ldd"

    # Create dynamic linker runtime file taking -$(ARCH) as reference
    for file in "${destdir}"/lib/ld-musl-*.so.1
    do
        ld_path="${file##*/}"			# Basename
        ld_path="${ld_path%%.so.1}.path"	# Get the rid of .so.1
        export ld_path
        break;
    done
    unset -v file

    if test -n "$ld_path"
    then
        mkdir -p "${destdir}/etc"

        if test -n "$libSuffix"
        then
            cat << EOF > "${destdir}"/etc/$ld_path
/lib
/lib${libSuffix}
/usr/local/lib
/usr/local/lib${libSuffix}
/usr/lib
/usr/lib${libSuffix}
/usr/$(gcc -dumpmachine)/lib
/opt/trinity/lib
/opt/trinity/lib${libSuffix}
/opt/trinity/lib/trinity
/opt/trinity/lib${libSuffix}/trinity
EOF
        else
            cat << EOF > "${destdir}"/etc/$ld_path
/lib
/usr/local/lib
/usr/lib
/usr/$(gcc -dumpmachine)/lib
/opt/trinity/lib
/opt/trinity/lib/trinity
EOF
        fi

        chmod 644 "${destdir}"/etc/$ld_path
        touch "${destdir}/etc/.graft-config"
    else
        echo "WARNING: \$ld_path is empty." 1>&2
    fi

    # Add to the post-install script a symlink for compatibility,
    # as some 'configure' scripts still have the Glibc name "ld.so.conf".

    mkdir -p "${destdir}/var/lib/qi"
    cat << EOF > "${destdir}/var/lib/qi/${full_pkgname}.sh"

# Adjust the toolchain only once (as needed)
if test -x /tools/bin/adjust-toolchain
then
    /tools/bin/adjust-toolchain && chmod 644 /tools/bin/adjust-toolchain
fi

# A symlink for compatibility
if test ! -e "\${rootdir}/etc/ld.so.conf"
then
    ( cd -- "\${rootdir}/etc" && ln -sf $ld_path ld.so.conf )
fi

EOF

    unset -v ld_path

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

